//
//  FLLPresentViewController.m
//  FLLGodOfCooking
//
//  Created by LL.F on 16/9/2.
//  Copyright © 2016年 LL.F. All rights reserved.
//

#import "FLLPresentViewController.h"
#import "ZWYNetTool.h"
#import "AFNetworking.h"
#import "FLLPresentModel.h"
#import "PresentBanner.h"
#import "PresentSecData.h"
#import "PresentShequ_Marrow.h"
#import "PresentShequ_Talent.h"
#import "PresentShequ_Topics.h"
#import "PresentData.h"
#import "Masonry.h"
#import "FLLPresentTableViewCell.h"
#import "FLLMacro.h"
#import "FLLPresentFirstTableViewCell.h"
#import "FLLPresentTopicsTableViewCell.h"
#import "FLLCycleImage.h"
#import "FLLWorksDetailViewController.h"
#import "FLLNewestViewController.h"


@interface FLLPresentViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *presentTableView;
@property (nonatomic, strong) FLLPresentModel *fllPresentModel;
@property (nonatomic, strong) FLLCycleImage *presentCycleImage;
@property (nonatomic, strong) UIView *cycleView;
@end

@implementation FLLPresentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 网络请求
    [self createPOSTRequest];
    // 创建tableView
    [self createPresentTableView];
    // 创建头视图
    [self createHeaderView];
    
   
}
- (void)createPOSTRequest
{
    [ZWYNetTool POST:@"http://api.izhangchu.com/ HTTP/1.1" andBody:@"methodName=ShequRecommend&version=4.40&" andBodyStyle:ZWYBodyString andHeader:@{@"Content-Type":@"application/x-www-form-urlencoded; charset=utf-8"} andResponse:ZWYJSON andSuccessBlock:^(id result) {
        self.fllPresentModel = [FLLPresentModel modelWithDic:result];
//        NSLog(@"%@", self.fllPresentModel.data.shequ_talent[0].nick);
        self.block();
        [self.presentTableView reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];


}
- (void)createPresentTableView
{

    self.presentTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    [self.view addSubview:_presentTableView];
    [self.presentTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.mas_equalTo(0);
    }];
    _presentTableView.dataSource = self;
    _presentTableView.delegate = self;
    _presentTableView.separatorStyle = UITableViewCellSelectionStyleNone;
    [_presentTableView registerNib:[UINib nibWithNibName:@"FLLPresentFirstTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"presentFirstTableCell"];
    [_presentTableView registerNib:[UINib nibWithNibName:@"FLLPresentTopicsTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"presentTopicsTableCell"];
    [_presentTableView registerNib:[UINib nibWithNibName:@"FLLPresentTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"presentTableCell"];
}
- (void)createHeaderView
{

    
    self.cycleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT * 0.2)];
    self.presentCycleImage = [[FLLCycleImage alloc] initWithFrame:CGRectMake(0, 0, _cycleView.bounds.size.width, _cycleView.bounds.size.height)];
    [_presentCycleImage removeTimer];
    [_cycleView addSubview:_presentCycleImage];

    // 轮播图赋值
    __weak FLLPresentViewController *weakSelf = self;
    self.block = ^(){
        weakSelf.presentCycleImage.mainImageArray = (NSMutableArray *)weakSelf.fllPresentModel.data.banner;
        
    };
    
    self.presentTableView.tableHeaderView = _cycleView;
    
    
}
#pragma mark -- TableView DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return self.fllPresentModel.data.shequ_topics.count + 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{


    if (indexPath.row == 0) {
        FLLPresentFirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"presentFirstTableCell"];
        cell.grayLabel.backgroundColor = FLLLightGrayColor;
        cell.titleLabel.text = @"达人";
        cell.titleLabel.font = [UIFont systemFontOfSize:16 * kWidthScale];
        NSArray<PresentShequ_Talent *> *array = self.fllPresentModel.data.shequ_talent;
        cell.array = array;
        cell.moreButton.tag = indexPath.row;
        [cell.moreButton addTarget:self action:@selector(didClickedMoreButton:) forControlEvents:UIControlEventTouchUpInside];
        return cell;
    }
    if (indexPath.row == 1) {
        FLLPresentTopicsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"presentTopicsTableCell"];
        cell.titleLabel.font = [UIFont systemFontOfSize:16 * kWidthScale];
        cell.grayLabel.backgroundColor = FLLLightGrayColor;
        NSArray<PresentShequ_Marrow *> *marrowArray = self.fllPresentModel.data.shequ_marrow;
        cell.marrowArray = marrowArray;
        
        cell.idBlock = ^(NSString *idString){
        
            FLLWorksDetailViewController *worksVC = [[FLLWorksDetailViewController alloc] init];
            worksVC.idString = idString;
            [self.navigationController pushViewController:worksVC animated:YES];
        };
        
    
        
        
        cell.moreButton.tag = indexPath.row;
        [cell.moreButton addTarget:self action:@selector(didClickedMoreButton:) forControlEvents:UIControlEventTouchUpInside];
        return cell;
    }
        FLLPresentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"presentTableCell"];
    
    
    cell.idBlock = ^(NSString *idString){
        FLLWorksDetailViewController *worksVC = [[FLLWorksDetailViewController alloc] init];
        
        worksVC.idString = idString;
        [self.navigationController pushViewController:worksVC animated:YES];
        
    };
    
        cell.iconImageVIew.image = [UIImage imageNamed:@"标签"];
        cell.grayLabel.backgroundColor = FLLLightGrayColor;
        cell.titleLabel.text = self.fllPresentModel.data.shequ_topics[indexPath.row - 2].topic_name;
        NSArray<PresentSecData *> *topicsArray = self.fllPresentModel.data.shequ_topics[indexPath.row - 2].data;
        cell.titleLabel.font = [UIFont systemFontOfSize:16 * kWidthScale];
        cell.topicsArray = topicsArray;
        cell.moreButton.tag = indexPath.row;
        [cell.moreButton addTarget:self action:@selector(didClickedMoreButton:) forControlEvents:UIControlEventTouchUpInside];

        return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return 200 * kHeightScale;
}
- (void)didClickedMoreButton:(UIButton *)button
{

    if (button.tag == 0) {
        
    } else if (button.tag == 1) {
        FLLNewestViewController *newestVC = [[FLLNewestViewController alloc] init];
        NSInteger is_marrow = 1;
        newestVC.is_marrow = is_marrow;
        newestVC.titleLabel = @"精选作品";
        [self.navigationController pushViewController:newestVC animated:YES];

    } else {
    FLLNewestViewController *newestVC = [[FLLNewestViewController alloc] init];
    newestVC.titleLabel = self.fllPresentModel.data.shequ_topics[button.tag - 2].topic_name;
    newestVC.topicID = self.fllPresentModel.data.shequ_topics[button.tag - 2].id_Data;
    [self.navigationController pushViewController:newestVC animated:YES];
        
    }

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
